package demo.jdk8;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;
import java.util.TimeZone;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class JodaDateUtil {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DateTimeFormatter ZONE_FORMATTER = DateTimeFormat
			      .forPattern("zzz");
		
		org.joda.time.DateTimeZone zone=  DateTimeZone.getDefault();
		 DateTime dt2 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("2012-12-26 03:27:39");  
		 System.err.println(ZONE_FORMATTER.print(System.currentTimeMillis()));   
		 System.err.println(zone.getShortName(0));
	     System.err.println(dt2.getZone().getShortName(0));
	       
	        
//    String timezone = "Asia/Shanghai";
//    System.setProperty("user.timezone", timezone);
//    TimeZone.setDefault(TimeZone.getTimeZone(timezone));
	   
	        DateTimeTest();
		
		DateTime dateTime = new DateTime(2015, 8, 31, 0, 0, 0);
		System.out.println(dateTime.plusDays(90).toString("yyyy-MM-dd"));
		System.out.println(dateTime.plusMonths(1).toString("yyyy-MM-dd"));
		System.out.println(dateTime.plusMonths(1).dayOfWeek().withMaximumValue().toString("E MM/dd/yyyy HH:mm:ss.SSS"));
		
		LocalDate now = new LocalDate();
		LocalDate lastDayOfPreviousMonth = now.minusMonths(1).dayOfMonth().withMaximumValue();
		
		LocalDate electionDate = now.monthOfYear()
		 .setCopy(11)        // November
		 .dayOfMonth()       // Access Day Of Month Property
		 .withMinimumValue() // Get its minimum value
		 .plusDays(6)        // Add 6 days
		 .dayOfWeek()        // Access Day Of Week Property
		 .setCopy("Monday")  // Set to Monday (it will round down)
		 .plusDays(1);       // Gives us Tuesday
	}  
	

	@SuppressWarnings("unused")
	public static void DateTimeTest(){
        //初始化时间  
        DateTime dateTime=new DateTime(2012, 12, 13, 18, 23,55);  
          
        // 年,月,日,时,分,秒,毫秒    
        DateTime dt3 = new DateTime(2011, 2, 13, 10, 30, 50, 333);// 2010年2月13日10点30分50秒333毫秒  
          
        //下面就是按照一点的格式输出时间  
        String str2 = dateTime.toString("MM/dd/yyyy hh:mm:ss.SSSa");  
        String str3 = dateTime.toString("dd-MM-yyyy HH:mm:ss");  
        String str4 = dateTime.toString("EEEE dd MMMM, yyyy HH:mm:ssa");  
        String str5 = dateTime.toString("MM/dd/yyyy HH:mm ZZZZ");  
        String str6 = dateTime.toString("MM/dd/yyyy HH:mm Z");  
          
        DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");  
        //时间解析    
        DateTime dateTime2 = DateTime.parse("2012-12-21 23:22:45", format);    
            
        //时间格式化，输出==> 2012/12/21 23:22:45 Fri    
        String string_u = dateTime2.toString("yyyy/MM/dd HH:mm:ss EE");    
        System.out.println(string_u);    
            
        //格式化带Locale，输出==> 2012年12月21日 23:22:45 星期五    
        String string_c = dateTime2.toString("yyyy年MM月dd日 HH:mm:ss EE",Locale.CHINESE);    
        System.out.println(string_c);  
          
        DateTime dt1 = new DateTime();// 取得当前时间  
           
        // 根据指定格式,将时间字符串转换成DateTime对象,这里的格式和上面的输出格式是一样的    
        DateTime dt2 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("2012-12-26 03:27:39");  
        System.err.println(dt2.getZone());
        //计算两个日期间隔的天数  
        LocalDate start=new LocalDate(2012, 12,14);
        LocalDate end=new LocalDate(2013, 01, 15);
        int days = Days.daysBetween(start, end).getDays();
          
        //计算两个日期间隔的小时数,分钟数,秒数  
          
        //增加日期  
        DateTime dateTime1 = DateTime.parse("2012-12-03");  
        dateTime1 = dateTime1.plusDays(30);  
        dateTime1 = dateTime1.plusHours(3);  
        dateTime1 = dateTime1.plusMinutes(3);  
        dateTime1 = dateTime1.plusMonths(2);  
        dateTime1 = dateTime1.plusSeconds(4);  
        dateTime1 = dateTime1.plusWeeks(5);  
        dateTime1 = dateTime1.plusYears(3);  
          
        // Joda-time 各种操作.....    
        dateTime = dateTime.plusDays(1) // 增加天    
                            .plusYears(1)// 增加年    
                            .plusMonths(1)// 增加月    
                            .plusWeeks(1)// 增加星期    
                            .minusMillis(1)// 减分钟    
                            .minusHours(1)// 减小时    
                            .minusSeconds(1);// 减秒数  
          
        //判断是否闰月    
        DateTime dt4 = new DateTime();    
        org.joda.time.DateTime.Property month = dt4.monthOfYear();    
        System.out.println("是否闰月:" + month.isLeap());  
          
        //取得 3秒前的时间    
        DateTime dt5 = dateTime1.secondOfMinute().addToCopy(-3);    
        dateTime1.getSecondOfMinute();// 得到整分钟后，过的秒钟数    
        dateTime1.getSecondOfDay();// 得到整天后，过的秒钟数    
        dateTime1.secondOfMinute();// 得到分钟对象,例如做闰年判断等使用  
          
        // DateTime与java.util.Date对象,当前系统TimeMillis转换    
        DateTime dt6 = new DateTime(new Date());    
        Date date = dateTime1.toDate();    
        DateTime dt7 = new DateTime(System.currentTimeMillis());    
        dateTime1.getMillis();   
          
        Calendar calendar = Calendar.getInstance();    
        dateTime = new DateTime(calendar); 
	}
	
	public static void calIntervalJoda(){
		while(true){  
			String birthday = "2012-3-10 08:20:55";  
            Scanner s = new Scanner(System.in);  
            System.out.println("########################################");  
            DateTimeFormatter format1 = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");  
            DateTimeFormatter format2 = DateTimeFormat .forPattern("yyyy-MM-dd");  
            DateTime startDateTime = DateTime.parse(birthday, format1);  
            System.out.println("宝宝来到这个世界已经");  
            calDateToDay(startDateTime,new DateTime());  
            System.out.println("如选择其它日期请输入(格式例如:2012-11-08 14:24:54或着2012-11-08)");  
            System.out.println("########################################");  
            String endDate = s.nextLine();  
            DateTime endDateTime = null;  
            try{  
                endDateTime = DateTime.parse(endDate,format1);  
            }catch(Exception e){  
                try{  
                    endDateTime = DateTime.parse(endDate,format2);  
                }catch(Exception e1){  
                    System.out.println("输入格式错误!请重新输入.");  
                    continue;  
                }  
            }  
            System.out.println("宝宝从出生到" + endDateTime.toString("yyyy-MM-dd HH:mm:ss") + "已经");  
            calDateToDay(startDateTime,endDateTime);  
        }  
	}
	
	public static void calDateToDay(DateTime startDateTime,DateTime endDateTime){  
       LocalDate start=new LocalDate(startDateTime);    
       LocalDate end=new LocalDate(endDateTime);  
       Days days = Days.daysBetween(start, end);  
       int intervalDays = days.getDays();  
       int intervalHours = endDateTime.getHourOfDay() - startDateTime.getHourOfDay();  
       int intervalMinutes = endDateTime.getMinuteOfHour() - startDateTime.getMinuteOfHour();  
       int intervalSeconds = endDateTime.getSecondOfMinute() - startDateTime.getSecondOfMinute();  
         
         
       if(intervalSeconds < 0){  
           intervalMinutes = intervalMinutes -1;  
           intervalSeconds = 60 + intervalSeconds;  
       }  
         
       if(intervalMinutes < 0){  
           intervalHours = intervalHours -1;  
           intervalMinutes = 60 + intervalMinutes;  
       }  
         
       if(intervalHours < 0){  
           intervalDays = intervalDays -1;  
           intervalHours = 24 + intervalHours;  
       }  
                 
       System.out.println(intervalDays + "天" + intervalHours +   
               "小时" + intervalMinutes + "分钟" + intervalSeconds + "秒");  
       System.out.println("############################");  
   }  

}
